<?php

namespace App\Services\Third;

use App\Exceptions\ExceptionHelper;
use App\Exceptions\ExceptionSms;
use App\Models\ThirdUse;

class VoiceNotify
{
    private $srvUrl  = 'https://yytz.market.alicloudapi.com/chuangxin/yytz';
    private $appCode = '';

    public static $errorCodeMap = [
        1   => '消息包格式错误',
        5   => '其他错误',
        12  => '号码不能为空',
        20  => '内容不能为空',
        100 => '系统内部错误',
    ];

    public function __construct($appCode)
    {
        $this->appCode = $appCode;
    }

    public function sendNotify($mobile)
    {
        ThirdUse::addVoiceUsedNum();
        $body = "content=%E6%82%A8%E5%A5%BD%EF%BC%8C%E6%82%A8%E7%9A%84%E5%BF%AB%E9%80%92%E5%B7%B2%E9%80%81%E8%BE%BE%E5%B0%8F%E5%8C%BA%E9%97%A8%E5%8F%A3%EF%BC%8C%E8%AF%B7%E5%89%8D%E6%9D%A5%E7%AD%BE%E6%94%B6%E3%80%82&mobile={$mobile}";
        return $this->http_call($this->srvUrl, $body, $this->appCode);
    }

    function http_call($url, $body, $appcode)
    {
        $method  = "GET";
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        $url  = $url . "?" . $body;
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $body);

        $response    = curl_exec($curl);
        $status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
        $resp_body   = substr($response, $header_size);
        \Log::info("语音通知接口，响应结果：" . json_encode($response, JSON_UNESCAPED_UNICODE));
        \Log::info("语音通知接口，状态码：" . $status_code);
        if ($status_code != 200) {
            $resp_headers = substr($response, 0, $header_size);
            $resp_headers = explode("\r\n", $resp_headers);
            \Log::info("语音通知接口，响应头：" . json_encode($resp_headers, JSON_UNESCAPED_UNICODE));
            foreach ($resp_headers as $header) {
                if (stripos($header, "X-Ca-Error-Message") === 0) {
                    ExceptionSms::returnError('请求错误');
                }
            }
            if ($resp_body) {
                $result = json_decode($resp_body, true);
                \Log::info("语音通知接口：" . json_encode($result, JSON_UNESCAPED_UNICODE));
                $status = $result['status'] ?? null;
                $msg    = $result['errmsg'] ?? null;
                if (in_array($status, [-11005, -211018])) {
                    throw ExceptionHelper::getInstance(ExceptionHelper::IMAGE_INVALID);
                } else {
                    $msg = $msg ? $msg : (self::$errorCodeMap[$status] ?? "参数错误");
                    ExceptionSms::returnError($msg);
                }
            }
        }

        curl_close($curl);
        return json_decode($resp_body, true);
    }

}
